<template>
  <div class="WdCellGroupStyle">
    <el-form :model="$store.state.$currentproperties" ref="vForm" :rules="rules" class="lef" label-position="left"
      label-width="80px" size="small">
      <el-collapse v-model="activeNames">
        <el-collapse-item title="基本属性" name="1">
          <el-form-item label="组标题" prop="title">
            <el-input v-model="$store.state.$currentproperties.title" placeholder="请输入组标题"></el-input>
          </el-form-item>
        </el-collapse-item>

        <el-collapse-item title="单元格设置(可拖拽)" name="2">
          <draggable v-model="$store.state.$currentproperties.cells" item-key="index"
            style="background: #f7f8fa; padding:5px" animation="300" ghostClass="ghostClass">
            <template #item="{element, index}">
              <div style="margin-bottom: 10px">
                <van-cell-group>
                  <van-cell is-link>
                    <template #title>
                      <div style="font-weight: bold">
                        <van-icon :name="element.icon"></van-icon>
                        <span style="margin-left: 10px">{{ element.title }}</span>
                      </div>
                    </template>
                    <template #default>
                      <div>
                        <el-button type="primary" size="small" link @click="editCell(index)">编辑</el-button>
                        <el-button type="primary" size="small" link @click="deleteConfirm(index)">删除</el-button>
                      </div>
                    </template>
                  </van-cell>
                  <van-cell title="描述" :value="element.note" />
                  <van-cell title="右侧内容" :value="element.rightText" />
                </van-cell-group>
              </div>
            </template>
          </draggable>

          <div style="margin: 10px">
            <el-button size="large" style="width: 100%" type="primary" icon="plus" @click="addCell">添加单元格</el-button>
          </div>

          <el-dialog v-model="isShow" title="单元格设置" draggable>
            <el-form ref="cellForm" :model="cellItem" :rules="cellRules" size="small" label-width="80px">
              <el-form-item label="标题" prop="title">
                <el-input v-model="cellItem.title" placeholder="请输入标题"></el-input>
              </el-form-item>
              <el-form-item label="描述" prop="note">
                <el-input v-model="cellItem.note" placeholder="请输入描述"></el-input>
              </el-form-item>
              <el-form-item label="图标" prop="icon">
                <el-input v-model="cellItem.icon" placeholder="请输入图标名称"></el-input>
              </el-form-item>
              <el-form-item label="右侧内容" prop="rightText">
                <el-input v-model="cellItem.rightText" placeholder="请输入右侧内容"></el-input>
              </el-form-item>
              <el-form-item label="跳转链接" prop="url">
                <el-input v-model="cellItem.url" placeholder="请输入跳转链接"></el-input>
              </el-form-item>
            </el-form>
            <template #footer>
              <el-button @click="isShow = false">取消</el-button>
              <el-button type="primary" @click="saveCell">保存</el-button>
            </template>
          </el-dialog>
        </el-collapse-item>

        <el-collapse-item title="高级设置" name="3">
          <el-form-item label="节点样式" prop="customClass">
            <el-input v-model="$store.state.$currentproperties.Advanced.customClass"></el-input>
          </el-form-item>
          <el-form-item label="内容样式" prop="contentClass">
            <el-input v-model="$store.state.$currentproperties.Advanced.contentClass"></el-input>
          </el-form-item>
        </el-collapse-item>
      </el-collapse>
    </el-form>
  </div>
</template>

<script>
import draggable from 'vuedraggable'

export default {
  name: 'WdCellGroupStyle',
  components: {
    draggable
  },
  data() {
    return {
      activeNames: ['1', '2', '3'],
      isShow: false,
      currentIndex: -1,
      cellItem: {
        title: '',
        note: '',
        icon: '',
        rightText: '',
        url: ''
      },
      cellRules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }]
      },
      rules: {
        title: [{ required: true, message: '请输入组标题', trigger: 'blur' }]
      }
    }
  },
  created() {
   
  },
  methods: {
    addCell() {
      this.cellItem = {
        title: '新单元格',
        note: '',
        icon: 'user-o',
        rightText: '',
        url: ''
      }
      this.currentIndex = -1
      this.isShow = true
    },
    editCell(index) {
      this.cellItem = JSON.parse(JSON.stringify(
        this.$store.state.$currentproperties.cells[index]
      ))
      this.currentIndex = index
      this.isShow = true
    },
    saveCell() {
      this.$refs.cellForm.validate(valid => {
        if (valid) {
          const cellCopy = JSON.parse(JSON.stringify(this.cellItem));
          if (this.currentIndex >= 0) {
            this.$store.state.$currentproperties.cells.splice(this.currentIndex, 1, cellCopy);
          } else {
            this.$store.state.$currentproperties.cells.push(cellCopy);
          }
          this.isShow = false;
        }
      });
    },
    deleteConfirm(index) {
      this.$confirm('确定删除此单元格吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.state.$currentproperties.cells.splice(index, 1)
      })
    }
  }
}
</script>

<style scoped lang="scss">
.WdCellGroupStyle {
  .lef {
    ::v-deep .el-form-item__label {
      text-align: left;
    }
  }
  
  .ghostClass {
    background-color: #f0f7ff !important;
  }
}
</style>
